#include "user_interact_window.h"
#include "ui_user_interact_window.h"
#include "socketandssl.h"
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QValueAxis>
#include <QtCharts>
#include <QLineSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>
#include "Mymodbus.h"



user_Interact_Window::user_Interact_Window(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::user_Interact_Window)
{
    ui->setupUi(this);

    pthread_mutex_init(&mutex, NULL);

    ui->star_friends_folder_scrollArea->hide();
    ui->my_devices_folder_scrollArea->hide();
    ui->my_contacts_folder_scrollArea->hide();

    ui->chat_info_widget->hide();

    ui->chat_scrollArea->hide();

    ui->chat_message_send_widget->hide();


    ui->add_contacts_pushButton->setStyleSheet("QPushButton {font-size: 40px;"
                                               "background-color:rgba(50,50,50,60);"
                                               "border-radius:30px;}"


                                               "QPushButton:hover {font-size: 40px;"
                                               "background-color:rgba(50,50,50,160);"
                                               "border-radius:30px;}");

    ui->solo_folder_pushButton->setStyleSheet("QPushButton {background-color:rgba(50,50,50,160);"
                                               "padding-right: 21;}"


                                               "QPushButton:hover {background-color:rgba(120,120,120,160);"
                                               "padding-right: 21;}");

    ui->p2p_folder_pushButton->setStyleSheet("QPushButton {background-color:rgba(50,50,50,160);"
                                              "padding-right: 21;}"


                                              "QPushButton:hover {background-color:rgba(120,120,120,160);"
                                              "padding-right: 21;}");

    ui->star_friends_folder_pushButton->setStyleSheet("QPushButton {padding-right: 21;"
                                              "background-color:rgba(0,150,50,60);}"


                                              "QPushButton:hover {padding-right: 21;"
                                              "background-color:rgba(0,150,50,90);}");

    ui->my_devices_folder_pushButton->setStyleSheet("QPushButton {padding-right: 18;"
                                                      "background-color:rgba(0,150,50,70);}"


                                                      "QPushButton:hover {padding-right: 18;"
                                                      "background-color:rgba(0,150,50,100);}");

    ui->my_contacts_folder_pushButton->setStyleSheet("QPushButton {padding-right: 18;"
                                                      "background-color:rgba(0,150,50,80);}"


                                                      "QPushButton:hover {padding-right: 18;"
                                                      "background-color:rgba(0,150,50,110);}");

    ui->my_growth_pushButton->setStyleSheet("QPushButton {padding-right:120;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:130;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->my_learning_path_pushButton->setStyleSheet("QPushButton {padding-right:65;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:75;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->my_project_warehouse_pushButton->setStyleSheet("QPushButton {padding-right:10;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:20;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->my_expriment_pushButton->setStyleSheet("QPushButton {padding-right:100;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:110;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->my_practice_pushButton->setStyleSheet("QPushButton {padding-right:120;"
                                               "font-size: 20px;"
                                               "background-color:rgba(50,50,50,60);"
                                               "border-radius:15px;}"


                                               "QPushButton:hover {padding-right:130;"
                                               "font-size: 20px;"
                                               "background-color:rgba(50,50,50,160);"
                                               "border-radius:15px;}");

    ui->release_project_pushButton->setStyleSheet("QPushButton {padding-right:80;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:90;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->free_talk_pushButton->setStyleSheet("QPushButton {padding-right:145;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:155;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->community_pushButton->setStyleSheet("QPushButton {padding-right:120;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:130;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->Competition_pushButton->setStyleSheet("QPushButton {padding-right:115;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "border-radius:15px;}"


                                            "QPushButton:hover {padding-right:125;"
                                            "font-size: 20px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "border-radius:15px;}");

    ui->get_warehouse_pushButton->setStyleSheet("QPushButton {padding-right:90;"
                                              "font-size: 20px;"
                                              "background-color:rgba(50,50,50,60);"
                                              "border-radius:15px;}"


                                              "QPushButton:hover {padding-right:100;"
                                              "font-size: 20px;"
                                              "background-color:rgba(50,50,50,160);"
                                              "border-radius:15px;}");

    ui->email_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                        "background-color:rgba(250,250,250,100);"
                                        "border-radius:15px;"
                                        "background-image:url(:/icon/message_Icon_ps_fixed_small.png);}"


                                        "QPushButton:hover {font-size: 20px;"
                                        "background-color:rgba(250,250,250,150);"
                                        "border-radius:15px;"
                                        "background-image:url(:/icon/message_Icon_ps_fixed_small.png);}");

    ui->collection_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                             "background-color:rgba(250,250,250,100);"
                                             "border-radius:15px;"
                                             "background-image:url(:/icon/star_Icon_ps_fixed_small.png);}"


                                             "QPushButton:hover {font-size: 20px;"
                                             "background-color:rgba(250,250,250,150);"
                                             "border-radius:15px;"
                                             "background-image:url(:/icon/star_Icon_ps_fixed_small.png);}");

    ui->settings_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                           "background-color:rgba(250,250,250,100);"
                                           "border-radius:15px;"
                                           "background-image:url(:/icon/settings_Icon_ps_fixed_small.png);}"


                                           "QPushButton:hover {font-size: 20px;"
                                           "background-color:rgba(250,250,250,150);"
                                           "border-radius:15px;"
                                           "background-image:url(:/icon/settings_Icon_ps_fixed_small.png);}");

    ui->settings_user_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                                "background-color:rgba(250,250,250,100);"
                                                "border-radius:15px;"
                                                "background-image:url(:/icon/settings_Icon_ps_fixed_small.png);}"


                                                "QPushButton:hover {font-size: 20px;"
                                                "background-color:rgba(50,50,50,60);"
                                                "border-radius:15px;"
                                                "background-image:url(:/icon/settings_Icon_ps_fixed_small.png);}");

    ui->my_device_computer_checkBox_1->setStyleSheet("QCheckBox::indicator {"
                                                     "width: 31px;"
                                                     "height: 31px;"
                                                     "background-image:url(:/icon/computer_Icon_ps_fixed_small.png)}"

                                                     "QCheckBox:hover {background-color:rgba(20,120,90,60);}");

    ui->my_device_computer_checkBox_2->setStyleSheet("QCheckBox::indicator {"
                                                     "width: 31px;"
                                                     "height: 31px;"
                                                     "background-image:url(:/icon/computer_Icon_ps_fixed_small.png)}"

                                                     "QCheckBox:hover {background-color:rgba(20,120,90,60);}");

    ui->my_device_computer_checkBox_3->setStyleSheet("QCheckBox::indicator {"
                                                     "width: 31px;"
                                                     "height: 31px;"
                                                     "background-image:url(:/icon/computer_Icon_ps_fixed_small.png)}"

                                                     "QCheckBox:hover {background-color:rgba(20,120,90,60);}");

    ui->backToMainWindow_pushButton->setStyleSheet("QPushButton {font-size: 20px;"
                                                   "background-color:rgba(250,250,250,240);"
                                                   "border-radius:20px;}"


                                                   "QPushButton:hover {font-size: 20px;"
                                                   "background-color:rgba(150,150,150,240);"
                                                   "border-radius:20px;}");

    ui->send_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,80);"
                                       "color:rgba(255,255,255,255);}"


                                       "QPushButton:hover {background-color:rgba(0,150,230,80);"
                                       "color:rgba(255,255,255,255);}");

    ui->send_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,80);"
                                       "color:rgba(255,255,255,255);}"


                                       "QPushButton:hover {background-color:rgba(0,150,230,80);"
                                       "color:rgba(255,255,255,255);}");

    ui->send_file_pushButton->setStyleSheet("QPushButton {border-radius:17px;"
                                            "background-color:rgba(50,50,50,60);"
                                            "background-image:url(:/icon/file_Icon_ps_fixed_36px.png);}"


                                            "QPushButton:hover {border-radius:17px;"
                                            "background-color:rgba(50,50,50,160);"
                                            "background-image:url(:/icon/file_Icon_ps_fixed_36px.png);}");

    ui->send_image_pushButton->setStyleSheet("QPushButton {border-radius:17px;"
                                             "background-color:rgba(50,50,50,60);"
                                             "background-image:url(:/icon/image_Icon_ps_fixed_36px.png);}"


                                             "QPushButton:hover {border-radius:17px;"
                                             "background-color:rgba(50,50,50,160);"
                                             "background-image:url(:/icon/image_Icon_ps_fixed_36px.png);}");

    ui->modbus_example_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,120);"
                                       "color:rgba(255,255,255,255);}"


                                       "QPushButton:hover {background-color:rgba(0,100,180,160);"
                                       "color:rgba(255,255,255,255);}");

    ui->modbus_example_all_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,120);"
                                                 "color:rgba(255,255,255,255);}"


                                                 "QPushButton:hover {background-color:rgba(0,100,180,160);"
                                                 "color:rgba(255,255,255,255);}");

    ui->sha256_example_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,120);"
                                                 "color:rgba(255,255,255,255);}"


                                                 "QPushButton:hover {background-color:rgba(0,100,180,160);"
                                                 "color:rgba(255,255,255,255);}");

    ui->start_collect_data_pushButton->setStyleSheet("QPushButton {background-color:rgba(0,100,180,150);"
                                                     "color:rgba(255,255,255,255);}"


                                                 "background-color:rgba(0,100,180,180);"
                                                 "color:rgba(255,255,255,255);}");

    ui->send_to_target_widget->hide();

    ui->recv_from_target_widget->hide();

    ui->time_style_label->hide();

    ui->My_expriment_scrollArea->hide();

    ui->exp_title_widget->hide();

    ui->exp_modbus_info_widget->hide();

    ui->modbus_main_widget->hide();

    ui->chat_info_widget->setGeometry(0,0,1130,101);

    ui->chat_message_send_widget->setGeometry(0,729,1131,221);

    ui->chat_scrollArea->setGeometry(0,99,1131,631);

    ui->My_expriment_scrollArea->setGeometry(-11,149,1451,811);

    ui->exp_title_widget->setGeometry(0,0,1451,161);

    ui->exp_modbus_info_widget->setGeometry(-10,0,1461,101);

    ui->modbus_main_widget->setGeometry(-21,99,1491,881);


    temperature_axisX = new QValueAxis();
    temperature_axisY = new QValueAxis();
    temperature_axisX->setTitleText("Time");
    temperature_axisY->setTitleText("Temperature");
    temperature_axisX->setMin(0);
    temperature_axisY->setMin(20);
    temperature_axisX->setMax(40);
    temperature_axisY->setMax(45);

    greenhouse_temperature_lineSeries = new QLineSeries();
    greenhouse_temperature_lineSeries->setPointsVisible(false);
    greenhouse_temperature_lineSeries->setName("Greenhouse");

    factory_temperature_lineSeries = new QLineSeries();
    factory_temperature_lineSeries->setPointsVisible(false);
    factory_temperature_lineSeries->setName("Factory");

    temperature_chart = new QChart();
    temperature_chart->addAxis(temperature_axisX, Qt::AlignBottom);
    temperature_chart->addAxis(temperature_axisY, Qt::AlignLeft);
    temperature_chart->addSeries(greenhouse_temperature_lineSeries);
    temperature_chart->addSeries(factory_temperature_lineSeries);
    temperature_chart->setAnimationOptions(QChart::SeriesAnimations);

    greenhouse_temperature_lineSeries->attachAxis(temperature_axisX);
    greenhouse_temperature_lineSeries->attachAxis(temperature_axisY);
    factory_temperature_lineSeries->attachAxis(temperature_axisX);
    factory_temperature_lineSeries->attachAxis(temperature_axisY);

    modbus_temperature_chartView = new QChartView();
    modbus_temperature_chartView->setRenderHint(QPainter::Antialiasing);

    modbus_temperature_chartView->setChart(temperature_chart);
    modbus_temperature_chartView->setRenderHint(QPainter::Antialiasing);

    modbus_temperature_chartView->setMinimumSize(700,350);
    modbus_temperature_chartView->setMaximumSize(700,350);

    ui->modbus_tem_charts_widget->layout()->addWidget(modbus_temperature_chartView);


    // Create a QChart object
    QChart *chart = new QChart();
    chart->setTitle("Average Temperature and Humidity");

    // Create a QChartView object
    QChartView *average_data_chartView = new QChartView(chart);
    average_data_chartView->setRenderHint(QPainter::Antialiasing);

    // Create two QBarSeries objects
    QBarSeries *temperatureSeries = new QBarSeries();
    QBarSeries *humiditySeries = new QBarSeries();

    // Create QBarSet objects for each object
    QBarSet *object1Temperature = new QBarSet("Factory tem");
    QBarSet *object2Temperature = new QBarSet("Greenhouse tem");
    QBarSet *object1Humidity = new QBarSet("Factory hum");
    QBarSet *object2Humidity = new QBarSet("Greenhouse hum");

    // Set values for each QBarSet object
    *object1Temperature << 45;
    *object2Temperature << 47;
    *object1Humidity << 50;
    *object2Humidity << 60;

    // Add QBarSet objects to QBarSeries objects
    temperatureSeries->append(object1Temperature);
    temperatureSeries->append(object2Temperature);
    humiditySeries->append(object1Humidity);
    humiditySeries->append(object2Humidity);

    // Add QBarSeries objects to QChart object
    chart->addSeries(temperatureSeries);
    chart->addSeries(humiditySeries);

    // Set x-axis, y-axis labels
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append("Temperature");
    axisX->append("Humidity");
    chart->createDefaultAxes();
    chart->setAxisX(axisX, temperatureSeries);

    average_data_chartView->setMinimumSize(500,350);
    average_data_chartView->setMaximumSize(500,350);

    ui->modbus_tem_charts_widget->layout()->addWidget(average_data_chartView);
    ui->modbus_tem_charts_widget->layout()->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    humidity_axisX = new QValueAxis();
    humidity_axisY = new QValueAxis();
    humidity_axisX->setTitleText("Time");
    humidity_axisY->setTitleText("Humidity");
    humidity_axisX->setMin(0);
    humidity_axisY->setMin(40);
    humidity_axisX->setMax(40);
    humidity_axisY->setMax(85);

    greenhouse_humidity_lineSeries = new QLineSeries();
    greenhouse_humidity_lineSeries->setPointsVisible(false);
    greenhouse_humidity_lineSeries->setName("Greenhouse");

    factory_humidity_lineSeries = new QLineSeries();
    factory_humidity_lineSeries->setPointsVisible(false);
    factory_humidity_lineSeries->setName("Factory");

    humidity_chart = new QChart();
    humidity_chart->addAxis(humidity_axisX, Qt::AlignBottom);
    humidity_chart->addAxis(humidity_axisY, Qt::AlignLeft);
    humidity_chart->addSeries(greenhouse_humidity_lineSeries);
    humidity_chart->addSeries(factory_humidity_lineSeries);
    humidity_chart->setAnimationOptions(QChart::SeriesAnimations);

    greenhouse_humidity_lineSeries->attachAxis(humidity_axisX);
    greenhouse_humidity_lineSeries->attachAxis(humidity_axisY);
    factory_humidity_lineSeries->attachAxis(humidity_axisX);
    factory_humidity_lineSeries->attachAxis(humidity_axisY);

    modbus_humidity_chartView = new QChartView();
    modbus_humidity_chartView->setRenderHint(QPainter::Antialiasing);

    modbus_humidity_chartView->setChart(humidity_chart);
    modbus_humidity_chartView->setRenderHint(QPainter::Antialiasing);

    modbus_humidity_chartView->setMinimumSize(600,300);
    modbus_humidity_chartView->setMaximumSize(600,300);

    ui->modbus_humidity_charts_widget->layout()->addWidget(modbus_humidity_chartView);

    ui->modbus_humidity_charts_widget->layout()->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));


    QPieSeries *series = new QPieSeries();
    series->append("Networks", 30);
    series->append("Hardwares", 20);
    series->append("Data structure", 10);
    series->append("Cryptography", 10);


    QPieSlice *slice = series->slices().at(0);
    slice->setExploded(true);
    slice->setLabelVisible();

    QPieSlice *slice1 = series->slices().at(1);
    slice1->setExploded(true);
    slice1->setLabelVisible();

    QPieSlice *slice2 = series->slices().at(2);
    slice2->setExploded(true);
    slice2->setLabelVisible();

    QChartView *chartView = new QChartView();
    chartView->setRenderHint(QPainter::Antialiasing);

    chartView->chart()->addSeries(series);
    chartView->chart()->setTitle("Expriment composition");

    chartView->setMinimumSize(500,350);
    chartView->setMaximumSize(500,350);

    ui->exp_charts_widget->layout()->addWidget(chartView);
    ui->exp_charts_widget->layout()->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    int client_socket = socketAndSSL::Creat_client_socket();

    socketAndSSL::Connet_to_server(client_socket);

    ssl = socketAndSSL::Sync_init_ssl(client_socket);

    socketAndSSL::Ssl_connect(client_socket, ssl);

    void* ssl_ptr = (void *)(ssl);
    pthread_t recv_thread;

    if(thread_flag == 1){

        recv_From_Server_Params *recv_from_server_param = (recv_From_Server_Params *)malloc(sizeof(recv_From_Server_Params));//MALLOC!!!!!!!!!!!!!!!WHEN USE STRUCTRUE NO MATTER IF IT IS PTR.(DIFF ENVIROMENT MAKE IT COMPLEX)

        recv_from_server_param->send_to_target_widget = ui->send_to_target_widget;
        recv_from_server_param->ssl_ptr = ssl_ptr;
        recv_from_server_param->this_ptr = (void *)this;

        //recv_From_Server_Params *recv_from_server_param_ptr = &recv_from_server_param;

        pthread_create(&recv_thread, NULL,user_Interact_Window::recv_from_server,(void *)recv_from_server_param);

        pthread_detach(recv_thread);

        thread_flag = 0;

        qDebug() << "thread_done.";
    }

    //if fail, try slot.
    connect(this, &user_Interact_Window::on_add_elements_signal_signal, this, &user_Interact_Window::on_add_elements_signal_recved);

}

// use slot to notify main thread create dialog
void user_Interact_Window::on_add_elements_signal_recved(){
    qDebug() << "trying add recv dialog";

    QWidget* recv_from_target_widget = new QWidget;
    recv_from_target_widget->setStyleSheet("background-color:rgba(0,0,0,0);");
    recv_from_target_widget->setMinimumSize(1101,81);
    recv_from_target_widget->setMaximumSize(1101,81);
    QHBoxLayout* layout = new QHBoxLayout(recv_from_target_widget);

    layout->setSpacing(20);

    QLabel* label = new QLabel();
    label->setMinimumSize(41,41);
    label->setMaximumSize(41,41);
    label->setStyleSheet("background-color:rgba(250,250,250,240);"
                         "color:rgba(55,55,55,125);"
                         "background-image:url(:/icon/computer_Icon_ps_fixed_41px.png);"
                         "border-radius:18px;");
    layout->addWidget(label);

    QTextBrowser* textBrowser = new QTextBrowser();
    textBrowser->setMinimumSize(411,51);
    textBrowser->setMaximumSize(411,51);
    textBrowser->setText(recv_message_buffer);
    textBrowser->setStyleSheet("background-color:rgba(255,255,255,255);"
                               "color:rgba(55,55,55,125);"
                               "font-size:20px;"
                               "padding-top:10px;");
    layout->addWidget(textBrowser);

    layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    recv_from_target_widget->setLayout(layout);


    //add time stamp
    QLabel* timeLabel = new QLabel();
    layout->addWidget(timeLabel);
    timeLabel->setMinimumSize(1117,17);
    timeLabel->setMaximumSize(1117,17);
    timeLabel->setStyleSheet("background-color:rgba(0,0,0,0);"
                             "color:rgba(255,255,255,125);"
                             "border-radius:0px;"
                             "padding-left:500px;");

    //time_widget_layout->addItem();

    quint64 dateTime = QDateTime::currentDateTime().toMSecsSinceEpoch();// to ms.
    QDateTime sysTime = QDateTime::fromMSecsSinceEpoch(dateTime);
    QString time_string = sysTime.toString(tr("yyyy-MM-dd-hh:mm:ss.zzz"));
    timeLabel->setText(time_string);

    qDebug() << QString(tr("time stamp：%1")).number(dateTime) << sysTime.toString(tr("yyyy-MM-dd-hh:mm:ss.zzz"));

    ui->scrollAreaWidgetContents_4->layout()->addWidget(timeLabel);
    this->ui->scrollAreaWidgetContents_4->layout()->addWidget(recv_from_target_widget);

    memset(recv_message_buffer, 0, 1024);

}

void* user_Interact_Window::recv_from_server(void *recv_from_server_param){

    recv_From_Server_Params *params = (recv_From_Server_Params *)recv_from_server_param;
    SSL *ssl = (SSL *)params->ssl_ptr;
    user_Interact_Window *this_ptr_inthread = static_cast<user_Interact_Window *>(params->this_ptr);

    while(true){

        //qDebug() << "message_recved.";
        int ret = SSL_read(ssl, recv_message_buffer, 1024);

        if(ret > 0){

            emit this_ptr_inthread->on_add_elements_signal_signal();
            qDebug() << "signal emited.";

            qDebug() << recv_message_buffer;

        }
    }
}

user_Interact_Window::~user_Interact_Window()
{
    delete ui;
}

void user_Interact_Window::mousePressEvent(QMouseEvent* event)
{
    mouse_pos = event->globalPos();
    window_pos = this->pos();
    diff_pos = mouse_pos - window_pos;
}

void user_Interact_Window::mouseMoveEvent(QMouseEvent* event)
{
    QPoint pos = event->globalPos();
    //this->move(pos);
    this->move(pos - diff_pos);
}

void user_Interact_Window::on_solo_folder_pushButton_clicked()
{

}


void user_Interact_Window::on_p2p_folder_pushButton_clicked(bool checked)
{
    static bool stat = checked;
    if(!stat){
        ui->p2p_folder_widget->hide();
        ui->p2p_folder_pushButton->setText("Don't wanna solo? Let's try:                                        ∧");
        stat = !stat;
    }else{
        ui->p2p_folder_widget->show();
        ui->p2p_folder_pushButton->setText("Don't wanna solo? Let's try:                                        ∨");
        stat = !stat;
    }
}


void user_Interact_Window::on_solo_folder_pushButton_clicked(bool checked)
{
    static bool stat = checked;
    if(!stat){
        ui->solo_folder_widget->hide();
        ui->solo_folder_pushButton->setText("Let's begin with:                                                             ∧");
        stat = !stat;
    }else{
        ui->solo_folder_widget->show();
        ui->solo_folder_pushButton->setText("Let's begin with:                                                             ∨");
        stat = !stat;
    }
}


void user_Interact_Window::on_star_friends_folder_pushButton_clicked(bool checked)
{
    static bool stat = !checked;
    if(!stat){
        ui->star_friends_folder_scrollArea->hide();
        ui->star_friends_folder_pushButton->setText("Star Friends                                                      ∧");
        stat = !stat;
    }else{
        ui->star_friends_folder_scrollArea->show();
        ui->star_friends_folder_pushButton->setText("Star Friends                                                      ∨");
        stat = !stat;
    }
}


void user_Interact_Window::on_my_devices_folder_pushButton_clicked(bool checked)
{
    static bool stat = !checked;
    if(!stat){
        ui->my_devices_folder_scrollArea->hide();
        ui->my_devices_folder_pushButton->setText("My Devices                                                       ∧");
        stat = !stat;
    }else{
        ui->my_devices_folder_scrollArea->show();
        ui->my_devices_folder_pushButton->setText("My Devices                                                       ∨");
        stat = !stat;
    }
}


void user_Interact_Window::on_my_contacts_folder_pushButton_clicked(bool checked)
{
    static bool stat = !checked;
    if(!stat){
        ui->my_contacts_folder_scrollArea->hide();
        ui->my_contacts_folder_pushButton->setText("My Contacts                                                     ∧");
        stat = !stat;
    }else{
        ui->my_contacts_folder_scrollArea->show();
        ui->my_contacts_folder_pushButton->setText("My Contacts                                                     ∨");
        stat = !stat;
    }
}


void user_Interact_Window::on_Quit_pushButton_clicked()
{
    this->close();
}


void user_Interact_Window::on_my_device_computer_checkBox_1_clicked()
{
    ui->chat_info_widget->show();

    ui->chat_scrollArea->show();

    ui->chat_message_send_widget->show();

    ui->chat_target_name_label->setText("    Kali-Linux 192.168.52.128");

    sprintf(target_IP, "192.168.52.128");
}


void user_Interact_Window::on_backToMainWindow_pushButton_clicked()
{
    ui->chat_info_widget->hide();

    ui->chat_scrollArea->hide();

    ui->chat_message_send_widget->hide();


}


void user_Interact_Window::on_my_device_computer_checkBox_2_clicked()
{
    ui->chat_info_widget->show();

    ui->chat_scrollArea->show();

    ui->chat_message_send_widget->show();

    ui->chat_target_name_label->setText("    Windows 192.168.43.1");

    sprintf(target_IP, "192.168.43.1");

}



void user_Interact_Window::on_my_device_computer_checkBox_3_clicked()
{
    ui->chat_info_widget->show();

    ui->chat_scrollArea->show();

    ui->chat_message_send_widget->show();

    ui->chat_target_name_label->setText("    Local Host Ubuntu-Linux 127.1");

    sprintf(target_IP, "127.0.0.1");
}


void user_Interact_Window::on_send_pushButton_clicked()
{
    QString user_message_send = ui->chat_plainTextEdit->toPlainText();
    QByteArray user_message_send_ByteArray = user_message_send.toLatin1();
    char *user_message_send_char_arr = user_message_send_ByteArray.data();

    char buffer[1024];
    int buffer_size;
    sprintf(buffer, user_message_send_char_arr);

    buffer[sizeof(buffer)] = '\0';

    static bool if_textBrowser_empty;
    if(ui->chat_plainTextEdit->toPlainText().isEmpty()){
        if_textBrowser_empty = true;
    }else{
        if_textBrowser_empty = false;
    }

    if(!if_textBrowser_empty){
        qDebug() << "sent!";

        static bool connect_stat = false;

        if(!connect_stat){
            SSL_write(ssl, "3_online", strlen("3_online"));
            connect_stat = !connect_stat;
        }

        SSL_write(ssl, buffer, strlen(buffer));

        ui->chat_plainTextEdit->clear();

        QWidget* send_to_target_widget = new QWidget;
        send_to_target_widget->setStyleSheet("background-color:rgba(0,0,0,0);");
        send_to_target_widget->setMinimumSize(1101,81);
        send_to_target_widget->setMaximumSize(1101,81);
        QHBoxLayout* layout = new QHBoxLayout(send_to_target_widget);

        layout->addSpacerItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

        QTextBrowser* textBrowser = new QTextBrowser();
        textBrowser->setMinimumSize(411,51);
        textBrowser->setMaximumSize(411,51);
        layout->addWidget(textBrowser);
        textBrowser->setText(user_message_send);
        textBrowser->setStyleSheet("background-color:rgba(255,255,255,255);"
                                   "color:rgba(55,55,55,125);"
                                   "font-size:20px;"
                                   "padding-top:10px;");

        QLabel* label = new QLabel();
        layout->addWidget(label);
        label->setMinimumSize(41,41);
        label->setMaximumSize(41,41);
        label->setStyleSheet("background-color:rgba(250,250,250,240);"
                             "color:rgba(55,55,55,125);"
                             "background-image:url(:/icon/computer_Icon_ps_fixed_41px.png);"
                             "border-radius:18px;");

        layout->setSpacing(20);

        send_to_target_widget->setLayout(layout);



        QWidget* time_widget = new QWidget;
        time_widget->setStyleSheet("background-color:rgba(0,0,0,0);");
        time_widget->setMinimumSize(1117,17);
        time_widget->setMaximumSize(1117,17);
        //QHBoxLayout* time_widget_layout = new QHBoxLayout(time_widget);

        QLabel* timeLabel = new QLabel();
        layout->addWidget(timeLabel);
        timeLabel->setMinimumSize(1117,17);
        timeLabel->setMaximumSize(1117,17);
        timeLabel->setStyleSheet("background-color:rgba(0,0,0,0);"
                                 "color:rgba(255,255,255,125);"
                                 "border-radius:0px;"
                                 "padding-left:500px;");

        //time_widget_layout->addItem();

        quint64 dateTime = QDateTime::currentDateTime().toMSecsSinceEpoch();// to ms.
        QDateTime sysTime = QDateTime::fromMSecsSinceEpoch(dateTime);
        QString time_string = sysTime.toString(tr("yyyy-MM-dd-hh:mm:ss.zzz"));
        timeLabel->setText(time_string);

        qDebug() << QString(tr("time stamp：%1")).number(dateTime) << sysTime.toString(tr("yyyy-MM-dd-hh:mm:ss.zzz"));

        ui->scrollAreaWidgetContents_4->layout()->addWidget(timeLabel);
        ui->scrollAreaWidgetContents_4->layout()->addWidget(send_to_target_widget);
    }


}


void user_Interact_Window::on_exp_backToMainWindow_pushButton_clicked()
{
    ui->My_expriment_scrollArea->hide();

    ui->exp_title_widget->hide();
}


void user_Interact_Window::on_my_expriment_pushButton_clicked()
{
    ui->My_expriment_scrollArea->show();

    ui->exp_title_widget->show();
}


void user_Interact_Window::on_modbus_example_pushButton_clicked()
{
    ui->exp_modbus_info_widget->show();

    ui->modbus_main_widget->show();
}


void user_Interact_Window::on_exp_backToMainWindow_pushButton_2_clicked()
{
    ui->exp_modbus_info_widget->hide();

    ui->modbus_main_widget->hide();
}


void user_Interact_Window::on_start_collect_data_pushButton_clicked()
{
    static bool if_modbus_connect_on = 0;

    if(if_modbus_connect_on != 1){


        modbus_t *maintain_ctx = Modbus::modbus_connect_to_server(modbus_ctx, tab_reg, modbus_rc);
        int count = greenhouse_temperature_lineSeries->points().size();
        greenhouse_temperature_lineSeries->append(QPointF(count, (rand() % 30)/10 + 10));
        factory_temperature_lineSeries->append(QPointF(count, (rand() % 30)/10 + 10));
        greenhouse_humidity_lineSeries->append(QPointF(count, (rand() % 30)/10 + 50));
        factory_humidity_lineSeries->append(QPointF(count, (rand() % 30)/10 + 50));

        modbus_ctx = maintain_ctx;

        ui->exp_monitor_1_stat_label->setText("・stat :    Active");
        ui->exp_monitor_2_stat_label->setText("・stat :    Active");

        ui->exp_monitor_1_stat_label->setStyleSheet("background-color:rgba(0,0,0,3);"
                                                    "color:rgba(0,200,0,150);"
                                                    "font-size:17px;");

        ui->exp_monitor_2_stat_label->setStyleSheet("background-color:rgba(0,0,0,3);"
                                                    "color:rgba(0,200,0,150);"
                                                    "font-size:17px;");

        QTimer *modbus_data_timer = new QTimer;
        modbus_data_timer->setInterval(1000);

        QObject::connect(modbus_data_timer,&QTimer::timeout,this,[=]{
            Modbus::collect_data_from_server(modbus_ctx, tab_reg, modbus_rc);
            int tem_time_count = greenhouse_temperature_lineSeries->points().size();
            if(tem_time_count > 40){
                temperature_axisX->setMax(tem_time_count);
            }
            int hum_time_count = greenhouse_humidity_lineSeries->points().size();
            if(hum_time_count > 40){
                humidity_axisX->setMax(hum_time_count);
            }
            greenhouse_temperature_lineSeries->append(QPointF(tem_time_count, tab_reg[0]));
            factory_temperature_lineSeries->append(QPointF(tem_time_count, tab_reg[2]));
            greenhouse_humidity_lineSeries->append(QPointF(hum_time_count, tab_reg[1]));
            factory_humidity_lineSeries->append(QPointF(hum_time_count, tab_reg[3]));

            ui->exp_monitor_1_temperature_label->setText(QString::number(tab_reg[0]));
            ui->exp_monitor_2_temperature_label->setText(QString::number(tab_reg[2]));
            ui->exp_monitor_1_humidity_label->setText(QString::number(tab_reg[1]));
            ui->exp_monitor_2_humidity_label->setText(QString::number(tab_reg[3]));
        });

        modbus_data_timer->start();

        if_modbus_connect_on = 1;
    }

}

